﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Xml;
using Meqs.Entity.Views;
using Meqs.Logic.Core;
using Meqs.Logic.Interface;

namespace Meqs.Logic.Implement.Oracle
{
    /// <summary>
    /// 评教系统报表逻辑层
    /// </summary>
    public class EvalReportLogic : BaseSqlMapDao, IEvalReportLogic
    {
        /// <summary>
        /// 获取答案列表
        /// </summary>
        /// <param name="evalSheetID"></param>
        /// <param name="qiId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public AnswerEntity GetAnswerList(int evalSheetID, int qiId, string schoolMark)
        {
            OracleConnection conn = new OracleConnection(ConnectionString);
            try
            {
                conn.Open();
                string sql = "select t.detail.getclobval() from {0}.SheetItems t where esid = {1}";
                sql = string.Format(sql, schoolMark, evalSheetID);

                OracleCommand cmd1 = conn.CreateCommand();
                cmd1.CommandText = sql;
                cmd1.CommandType = CommandType.Text;
                DataTable dt = new DataTable();
                OracleDataAdapter ad1 = new OracleDataAdapter(cmd1);
                ad1.Fill(dt);
                int count = dt.Rows.Count;
                if (count == 0)
                    return null;

                sql = "select qitype from {0}.questionitems where rid = {1}";
                sql = string.Format(sql, schoolMark, qiId);
                OracleCommand cmd2 = conn.CreateCommand();
                cmd2.CommandText = sql;
                cmd2.CommandType = CommandType.Text;
                DataTable d = new DataTable();
                OracleDataAdapter ad2 = new OracleDataAdapter(cmd2);
                ad2.Fill(d);
                int qiType = int.Parse(d.Rows[0][0].ToString());
                if (qiType == 1)
                {
                    sql = "select count(t.rid) from {0}.sheetitems t where t.esid = {1}";
                    sql = string.Format(sql, schoolMark, evalSheetID);
                    OracleCommand cmd3 = conn.CreateCommand();
                    cmd3.CommandText = sql;
                    cmd3.CommandType = CommandType.Text;
                    OracleDataAdapter ad3 = new OracleDataAdapter(cmd3);
                    ad3.Fill(d);
                    int replyNum = int.Parse(d.Rows[0][0].ToString());
                    // 问答题
                    IList<string> replyList = new List<string>();
                    foreach (DataRow row in dt.Rows)
                    {
                        string detail = row[0].ToString();
                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml(detail);

                        XmlNode replyNode = doc.SelectSingleNode("//items/item[@id=\"" + qiId + "\"]/reply");
                        if (replyNode == null) continue;
                        replyList.Add(replyNode.InnerXml);
                    }
                    AnswerEntity ae = new AnswerEntity(qiId, qiType, 0, 0, 0, replyNum, replyList);
                    return ae;
                }
                else
                {
                    // 选择、得分题
                    double allScore = 0;
                    double allMaxScore = 0;
                    foreach (DataRow row in dt.Rows)
                    {
                        string detail = row[0].ToString();
                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml(detail);

                        XmlNode scoreNode = doc.SelectSingleNode("//items/item[@id=\"" + qiId + "\"]/score");
                        if (scoreNode == null) continue;
                        double score = double.Parse(scoreNode.InnerXml);
                        allScore += score;

                        XmlNode maxNode = doc.SelectSingleNode("//items/item[@id=\"" + qiId + "\"]/max");
                        if (maxNode == null) continue;
                        double max = double.Parse(maxNode.InnerXml);
                        allMaxScore += max;
                    }
                    double avgScore = allScore / count;
                    double maxScore = allMaxScore / count;
                    double avgPercent = maxScore == 0 ? 0 : avgScore / maxScore;
                    AnswerEntity ae = new AnswerEntity(qiId, qiType, avgScore, avgPercent, maxScore, 0, null);
                    return ae;
                }
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                conn.Close();
            }

        }

        /// <summary>
        /// 获取答案列表
        /// </summary>
        /// <param name="studentEvalID"></param>
        /// <param name="qiId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public AnswerEntityExtend GetAnswerListByStudentEval(int studentEvalID, int studentID, int qiId, string schoolMark)
        {
            throw new NotImplementedException();
        }
    }
}
